﻿#ifndef QTERMINALDISPLAY_H
#define QTERMINALDISPLAY_H

#include <QObject>
#include <QWidget>
#include <QVector>
#include <QTimer>
#include <QPixmap>
#include "qTerminalWidget/qterminalscreen.h"
#include <QMenu>
#include <QAction>

enum praseType{
    text,
    csi,
    osc,
    esc,
    esc2,
    bracket,
};


#define  MAX(a,b) ((a)>(b)?(a):(b))
#define  MIN(a,b) ((a)<(b)?(a):(b))


class qTerminalDisplay : public QWidget
{
    Q_OBJECT
    praseType type=praseType::text;//解析的字符类型

    QTimer timerPaint;
    QTimer timer2Paint;
    QTimer timerResize;
    qTerminalScreen *screen=nullptr;
    QPixmap _backgroundImage;
    struct InputMethodData
    {
        QString preeditString;
        QRect previousPreeditRect;
    };
    InputMethodData _inputMethodData;
    void init();

    void praseChar(QByteArray data);
    int praseCsi(QString seq, qTerminalFont &terminalFont);
    QRect preeditRect() const ;//输入法显示区域
    int selectX=0,selectY=0,selectEndX=0,selectEndY=0;
    int selectRow=0,selectCol=0,selectEndRow=0,selectEndCol=0;
    bool selected=false;
    bool cursorHideFlag=false;
    QMenu* m_ContextMenu;
    void InitContextMenu();
    void copyContentToClipBoard();
    char copySelectContent(int,int);
    void pasteContent();
    void paintSelectContent(QPainter *painter, int current_row, int current_col, QRect rectangle);
public:
    explicit qTerminalDisplay(QWidget *parent = nullptr);
    ~qTerminalDisplay();
    bool keyPadMode=false;//案件的不同工作模式
    
signals:
    void setScrollValue(int min,int max,int page,int value);//设置滚动条参数
    void setScrollBarPos(int value);//鼠标滚轮更新滚动条位置
    void setScrollPageSignal(int page);
    void resizeSignal(int _row,int _col);//配置终端显示区域
    void sendDataStringSlgnal(QString data);
    void sendDataByteArrarySlgnal(QByteArray data);
private slots:
    void timerPaintOut();
    void customContextMenuRequestedSlots(const QPoint &pos);
public  slots:
    void recvData(QByteArray data);
    void sendData(QString data);
    void sliderMovedScrollBarPosition(int value);//鼠标拉动滚动条位置
    void valueChangedSlots(int value);
    int updateTimerSlots();//刷新界面
    void setFocusSlots();//获取焦点
    // QWidget interface
    void resizeSlots();
    void resizeSlotsTimer();
protected:
    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
    void paintEvent(QPaintEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;
    void inputMethodEvent(QInputMethodEvent *) override;
    QVariant inputMethodQuery(Qt::InputMethodQuery) const override;


    // QObject interface
public:
    bool event(QEvent *event) override;
};

#endif // QTERMINALDISPLAY_H
